﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace lhservice.common
{
  /// <summary>
  /// 公共的一些方法和属性
  /// </summary>
  public class PublicHelper
  {
    /// <summary>
    /// 客服消息中心服务器主机 例：http://app.lanpiankeji.com:8100/
    /// </summary>
    public static string Host {get;set;}

    /// <summary>
    /// 客户注册信息接口服务器主机
    /// </summary>
    public static string RegistHost { get; set; }

    /// <summary>
    ///  当前时间戳（字符串）
    /// </summary>
    /// <returns></returns>
    public static string GetTsNow()
    {
      return (DateTime.Now.Ticks - 621356256000000000).ToString();
    }

    /// <summary>
    ///  当前时间（字符串）
    /// </summary>
    /// <returns></returns>
    public static string GetDtNow()
    {
      TimeSpan ts = new TimeSpan(long.Parse(GetTsNow()));
      return TsToDt(ts);    
    }

    /// <summary>
    /// 时间戳输出格式化时间
    /// </summary>
    /// <param name="ts"></param>
    /// <returns></returns>
    public static string TsToDt(TimeSpan ts)
    {
      var utctime = new System.DateTime(1970, 1, 1);
      DateTime ldt = TimeZoneInfo.ConvertTime(utctime, TimeZoneInfo.Utc, TimeZoneInfo.Local);
      DateTime dt = ldt.Add(ts);
      return dt.ToString("yyyy-MM-dd HH:mm");
    }

    /// <summary>
    /// IBM850字符集转GBK
    /// </summary>
    /// <param name="ibm850Str"></param>
    /// <returns></returns>
    public static string Ibm850ToGbk(string ibm850Str) {
      var byteIbm850 = Encoding.GetEncoding("ibm850").GetBytes(ibm850Str);
      return Encoding.GetEncoding("gbk").GetString(byteIbm850);
    }
  }
}
